<template>
  <page-card title="菜单管理">
    <a-form layout="inline">
      <a-row type="flex"
             :gutter="16"
             style="margin-bottom:20px"
             justify="end">
        <a-col flex="1">
          <a-button type="primary"
                    icon="plus"
                    @click="handleEdit()">添加根级</a-button>
          &nbsp;
          <a-button v-if="this.hasPermission('edit')"
                    type="primary"
                    icon="edit"
                    @click="initMenuLevel()">修正菜单层级</a-button>
        </a-col>
        <a-col>
          <a-input-search v-model="queryParams.name"
                          placeholder="菜单名称"
                          allow-clear
                          @search="load" />
        </a-col>
        <a-col>
          <a-select v-model="queryParams.category"
                    @change="load">
            <a-select-option value="1">
              管理端
            </a-select-option>
            <a-select-option value="2">
              用户端
            </a-select-option>
          </a-select>
        </a-col>
      </a-row>
    </a-form>
    <a-table :columns="columns"
             :rowKey="(record, index) => record.id"
             :pagination="false"
             :data-source="datasource">
      <span slot="status"
            :style="{color:statusCell[record.status].color}"
            slot-scope="text, record">{{ statusCell[record.status].text }}</span>
      <span slot="action"
            slot-scope="text, record">
        <template v-for="(action, index) in actions">
          <span :key="index">
            <a-divider type="vertical" />
            <template v-if="action.code!='delete'">
              <span v-if="action.disableAction && action.disableAction(record) !== false"
                    class="x-grid-action-disable">
                {{ action.text }}
              </span>
              <a v-else
                 @click="action.fn(record)">
                {{ action.text }}
              </a>
            </template>
            <a-popconfirm v-else
                          placement="topRight"
                          :title="record.children&&record.children.length?'该菜单存在子级，确认删除？':'确认删除？'"
                          ok-text="是"
                          cancel-text="否"
                          @confirm="action.fn(record)">
              <a href="javascript:;">
                {{ action.text }}
              </a>
            </a-popconfirm>
          </span>
        </template>
      </span>
    </a-table>
  </page-card>
</template>

<script>

import kvmap from '../../kvmap'
import { getAll, del, initMenuLevel, deleteById } from '../../api/menu/index'
import { TABLE_STATUS_CELL } from '../../constants'
import ArrayUtil from '@zh/common-utils/utils/array'

export default {
  data () {
    return {
      // 高级搜索 展开/关闭
      advanced: false,
      deleteBtnDisabled: false,
      statusCell: TABLE_STATUS_CELL,
      datasource: [],
      queryParams: { category: '1' },
      actions: [],
      columns: [{
        title: '名称',
        flex: 1,
        dataIndex: 'name'
      }, {
        title: '编码',
        dataIndex: 'code',
        width: 200,
        ellipsis: true
      }, {
        title: '类型',
        align: 'center',
        width: 100,
        dataIndex: 'type',
        customRender: (v) => kvmap.menu.type[v]
      }, {
        title: '排序号',
        width: 100,
        align: 'center',
        dataIndex: 'orderNo'
      }, {
        title: '状态',
        align: 'center',
        width: 100,
        dataIndex: 'status',
        scopedSlots: { customRender: 'status' }
      }, {
        title: '路由地址',
        dataIndex: 'uri',
        ellipsis: true
      }]
    }
  },
  created () {
    let width = 130,
      me = this
    if (this.hasPermission('edit') && (width += 28)) {
      this.actions.push({
        text: '编辑',
        code: 'edit',
        fn (record) {
          me.handleEdit(null, record)
        }
      })
    }
    if (this.hasPermission('add') && (width += 28)) {
      this.actions.push({
        text: '添加子级',
        code: 'add',
        disableAction (record) {
          // 功能菜单禁止添加下级
          return record.type == '3'
        },
        fn (record) {
          me.handleEdit(record, null)
        }
      })
    }
    if (this.hasPermission('delete') && (width += 28)) {
      this.actions.push({
        text: '删除',
        code: 'delete',
        fn: this.handleDelete
      })
    }
    if (this.actions.length > 0) {
      this.columns.push({
        title: '操作',
        width: width,
        align: 'center',
        scopedSlots: { customRender: 'action' }
      })
    }
  },
  activated () {
    this.load()
  },
  methods: {
    initMenuLevel () {
      initMenuLevel().then(res => { })
    },
    load () {
      getAll({ ...this.queryParams }).then((response) => {
        response.map((i) => {
          i.parentId == '0' && (i.parentId = null)
        })
        const { nodeList } = ArrayUtil.toTree(response, 'id', 'parentId')
        this.datasource = nodeList
      })
    },
    handleEdit (parent, record) {
      this.$router.push({
        path: '/menu/edit',
        query: {
          parentId: parent?.id,
          parentName: parent?.name,
          id: record?.id,
          name: record?.name
        }
      })
    },
    handleDelete (record) {
      deleteById(record.id).then(() => {
        this.message.success('删除成功')
        this.load()
      })
    }
  }
}
</script>

<style lang="less">
</style>
